package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.dao.ExperimentMapper;
import com.simtop.expection.GlobalException;
import com.simtop.pojo.Ud;
import com.simtop.pojo.Uds;
import com.simtop.pojo.experiment.Experiment;
import com.simtop.pojo.experiment.ExperimentDynamicVo;
import com.simtop.pojo.experiment.ExperimentReportVo;
import com.simtop.service.CheckContent;
import com.simtop.service.ExperimentService;
import com.simtop.util.html.EscapeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class ExperimentServiceImpl implements ExperimentService {

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private CheckContent checkContent;

    /**
     * 添加实验报告
     * @param experiment
     * @return
     */
    @Override
    public boolean insertExperiment(Experiment experiment) {
        return experimentMapper.insertExperiment(experiment);
    }

    /**
     * 审核实验报告内容
     * @param experiment
     * @return
     */
    @Override
    public List<String> checkExperiment(Experiment experiment) {
        String clean = EscapeUtil.clean(experiment.getExperimentContent());
        int a = clean.length()/6666;
        if(clean.length()/6666!=0){
            a = a+1;
        }
        ArrayList<String> strings = new ArrayList<>(a);
        //判断是否超过审核的最大限制
        if(clean.length() > 6666){
            for (int i = 1; i <= a; i++) {
                if(i == a){
                    String b = checkContent.checkText(clean.substring((i-1)*6666,clean.length()));
                    if(b == null){
                        return null;
                    }
                    strings.add(b);
                }else{
                    String s = checkContent.checkText(clean.substring((i-1)*6666,i*6666));
                    if(s == null){
                        return null;
                    }
                    strings.add(s);
                }
            }
        }else{
            String s = checkContent.checkText(clean);
            if(s == null){
                return null;
            }
            strings.add(s);
        }
        for (String s : strings) {
            JSONObject parse = JSONObject.parseObject(s);
            if(!parse.getString("conclusionType").equals("1") && parse.getString("conclusion").equals("不合规")){
                throw new GlobalException("审核提交的内容失败",strings);
            }
        }
        return strings;
    }

    /**
     * 修改实验报告
     * @param experiment
     * @return
     */
    @Override
    public boolean updateExperiment(Experiment experiment) {
        return experimentMapper.updateExperiment(experiment);
    }

    /**
     * 作品动态列表
     * @param message 关键字
     * @return
     */
    @Override
    public List<ExperimentDynamicVo> getAllDynamic(String message) {
        return experimentMapper.getAllDynamic(message);
    }

    /**
     * 获取实验报告列表
     * @param userId
     * @param message 关键字
     * @return
     */
    @Override
    public List<ExperimentReportVo> getAllReport(Integer userId, String message) {
        return experimentMapper.getAllReport(userId,message);
    }

    /**
     * 点赞
     * @param userId
     * @param experimentId
     * @return
     */
    @Override
    public boolean giveLike(Integer userId, Long experimentId) {
        return experimentMapper.giveLike(userId, experimentId);
    }

    /**
     * 取消点赞
     * @param userId
     * @param experimentId
     * @return
     */
    @Override
    public boolean unGiveLike(Integer userId, Long experimentId) {
        return experimentMapper.unGiveLike(userId, experimentId);
    }

    /**
     * 删除实验报告
     * @param experimentId
     * @return
     */
    @Override
    public boolean delete(Long experimentId) {
        return experimentMapper.delete(experimentId);
    }


    @Override
    public void addud(int id, String username, Object one0, String oneformat1, String oneformat2, Object one3, Object one4, Object two0, String twoformat1, String twoformat2, Object two3, Object two4, Object three0, String threeformat1, String threeformat2, Object three3, Object three4, Object for0, String forformat1, String forformat2, Object for3, Object for4, Object five0, String fiveformat1, String fiveformat2, Object five3, Object five4, Object six0, String sixformat1, String sixformat2, Object six3, Object six4, Object seven0, String sevenformat1, String sevenformat2, Object seven3, Object seven4, Object eight0, String eightformat1, String eightformat2, Object eight3, Object eight4, Object nine0, String nineformat1, String nineformat2, Object nine3, Object nine4, Object ten0, String tenformat1, String tenformat2, Object ten3, Object ten4, Object eleve0, String eleveformat1, String eleveformat2, Object eleve3, Object eleve4, Object twelve0, String twelveformat1, String twelveformat2, Object twelve3, Object twelve4, Object thirteen0, String thirteenformat1, String thirteenformat2, Object thirteen3, Object thirteen4, Object fourteen0, String fourteenformat1, String fourteenformat2, Object fourteen3, Object fourteen4, Object fifteen0, String fifteenformat1, String fifteenformat2, Object fifteen3, Object fifteen4, Object sixteen0, String sixteenformat1, String sixteenformat2, Object sixteen3, Object sixteen4, Object seventeen0, String seventeenformat1, String seventeenformat2, Object seventeen3, Object seventeen4, Object eighteen0, String eighteenformat1, String eighteenformat2, Object eighteen3, Object eighteen4, Object nineteen0, String nineteenformat1, String nineteenformat2, Object nineteen3, Object nineteen4, Object twenty0, String twentyformat1, String twentyformat2, Object twenty3, Object twenty4, Object last0, String lastformat1, String lastformat2, Object last3, Object last4) {
        experimentMapper.addud(id, username, one0, oneformat1, oneformat2, one3, one4,
                two0, twoformat1, twoformat2, two3, two4,
                three0, threeformat1, threeformat2, three3, three4,
                for0, forformat1, forformat2, for3, for4,
                five0, fiveformat1, fiveformat2, five3, five4,
                six0, sixformat1, sixformat2, six3, six4,
                seven0, sevenformat1, sevenformat2, seven3, seven4,
                eight0, eightformat1, eightformat2, eight3, eight4,
                nine0, nineformat1, nineformat2, nine3, nine4,
                ten0, tenformat1, tenformat2, ten3, ten4,
                eleve0,eleveformat1,eleveformat2,eleve3,eleve4,
                twelve0,twelveformat1,twelveformat2,twelve3,twelve4,
                thirteen0,thirteenformat1,thirteenformat2,thirteen3,thirteen4,
                fourteen0,fourteenformat1,fourteenformat2,fourteen3,fourteen4,
                fifteen0,fifteenformat1,fifteenformat2,fifteen3,fifteen4,
                sixteen0,sixteenformat1,sixteenformat2,sixteen3,sixteen4,
                seventeen0,seventeenformat1,seventeenformat2,seventeen3,seventeen4,
                eighteen0,eighteenformat1,eighteenformat2,eighteen3,eighteen4,
                nineteen0,nineteenformat1,nineteenformat2,nineteen3,nineteen4,
                twenty0,twentyformat1,twentyformat2,twenty3,twenty4,
                last0, lastformat1, lastformat2, last3, last4


        );
    }

    @Override
    public List<Ud> allsy(int userid) {
        return experimentMapper.allsy(userid);
    }

    @Override
    public List<Ud> allallsy() {
        return experimentMapper.allallsy();
    }

    @Override
    public void addu3d02(int id, String username, String data, String formatdata) {
        experimentMapper.addu3d02(id,username,data,formatdata);
    }

    @Override
    public List<Uds> allsy2(int userid) {
        return experimentMapper.allsy2(userid);
    }

    @Override
    public List<Uds> allallsy02() {
        return experimentMapper.allallsy02();
    }

    @Override
    public void deleteExperimental(Integer udId) {
        experimentMapper.deleteExperimental(udId);
    }

}
